package com.ty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ty.entity.AdminEntity;
import com.ty.entity.AdminRoleEntity;
import com.ty.mapper.AdminMapper;
import com.ty.mapper.AdminRoleMapper;
import com.ty.req.UserLoginReq;
import com.ty.service.IAdminService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2024/1/21 16:05
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, AdminEntity> implements IAdminService {
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private AdminRoleMapper adminTypeMapper;
    @Override
    public AdminEntity login(UserLoginReq admin) {
        QueryWrapper<AdminEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AdminEntity::getUser, admin.getUser());
        List<AdminEntity> userList = adminMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(userList)) {
            return null;
        } else {
            QueryWrapper<AdminRoleEntity> roleWrapper = new QueryWrapper<>();
            QueryWrapper<AdminRoleEntity> adminType = roleWrapper.eq("id", userList.get(0).getRoleId());
            List<AdminRoleEntity> adminRole = adminTypeMapper.selectList(adminType);
            userList.get(0).setAdminRole(adminRole.get(0));
            return userList.get(0);
        }
    }
}
